<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>变量和常量 | Swift 简介</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.1.0">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="gitbook/style.css">
    
    

        
    
    
    <link rel="next" href="./03-optional.html" />
    
    
    <link rel="prev" href="./01-playground.html" />
    

        
    </head>
    <body>
        
        
    <div class="book" data-level="0.2" data-basepath="." data-revision="Fri Aug 28 2015 04:40:56 GMT+0800 (CST)">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
        
        
        

        

        
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                        <i class="fa fa-check"></i>
                        
                        Swift快速入门
                    </a>
                
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="0.1" data-path="01-playground.html">
            
                
                    <a href="./01-playground.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.1.</b>
                        
                        Playground
                    </a>
                
            
            
        </li>
    
        <li class="chapter active" data-level="0.2" data-path="02-var_and_let.html">
            
                
                    <a href="./02-var_and_let.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.2.</b>
                        
                        变量和常量
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.3" data-path="03-optional.html">
            
                
                    <a href="./03-optional.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.3.</b>
                        
                        Optional可选值
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.4" data-path="04-control_flow.html">
            
                
                    <a href="./04-control_flow.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.4.</b>
                        
                        控制流
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.5" data-path="05-loop.html">
            
                
                    <a href="./05-loop.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.5.</b>
                        
                        循环
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.6" data-path="06-string.html">
            
                
                    <a href="./06-string.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.6.</b>
                        
                        字符串
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.7" data-path="07-collection.html">
            
                
                    <a href="./07-collection.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.7.</b>
                        
                        集合
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.8" data-path="08-func.html">
            
                
                    <a href="./08-func.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.8.</b>
                        
                        函数
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.9" data-path="09-closure.html">
            
                
                    <a href="./09-closure.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.9.</b>
                        
                        闭包
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.10" data-path="10-init_func.html">
            
                
                    <a href="./10-init_func.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.10.</b>
                        
                        构造函数
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.11" data-path="11-lazy.html">
            
                
                    <a href="./11-lazy.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.11.</b>
                        
                        懒加载 &amp; didSet
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.12" data-path="12-ats.html">
            
                
                    <a href="./12-ats.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.12.</b>
                        
                        ATS
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                Published with GitBook
            </a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Table of Contents"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Font Settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Share"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">
                    Share on Twitter
                </button>
                <button type="button" data-sharing="google-plus" class="button">
                    Share on Google
                </button>
                <button type="button" data-sharing="facebook" class="button">
                    Share on Facebook
                </button>
                <button type="button" data-sharing="weibo" class="button">
                    Share on Weibo
                </button>
                <button type="button" data-sharing="instapaper" class="button">
                    Share on Instapaper
                </button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Google"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Twitter"><i class="fa fa-twitter"></i></a>
    
    
    


    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >Swift 简介</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="%E5%8F%98%E9%87%8F%E5%92%8C%E5%B8%B8%E9%87%8F">&#x53D8;&#x91CF;&#x548C;&#x5E38;&#x91CF;</h1>
<h2 id="%E5%AE%9A%E4%B9%89">&#x5B9A;&#x4E49;</h2>
<ul>
<li><code>let</code> &#x5B9A;&#x4E49;&#x5E38;&#x91CF;&#xFF0C;&#x4E00;&#x7ECF;&#x8D4B;&#x503C;&#x4E0D;&#x5141;&#x8BB8;&#x518D;&#x4FEE;&#x6539;</li>
<li><code>var</code> &#x5B9A;&#x4E49;&#x53D8;&#x91CF;&#xFF0C;&#x8D4B;&#x503C;&#x4E4B;&#x540E;&#x4ECD;&#x7136;&#x53EF;&#x4EE5;&#x4FEE;&#x6539;</li>
</ul>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> x = <span class="hljs-number">100</span>
<span class="hljs-comment">//: &#x5E38;&#x91CF;&#x4E00;&#x65E6;&#x8BBE;&#x7F6E;&#xFF0C;&#x4E0D;&#x80FD;&#x518D;&#x6B21;&#x4FEE;&#x6539;&#xFF0C;&#x4EE5;&#x4E0B;&#x4EE3;&#x7801;&#x4F1A;&#x62A5;&#x9519;</span>
<span class="hljs-comment">//x = 300</span>

<span class="hljs-keyword">var</span> y = <span class="hljs-number">200</span>
<span class="hljs-comment">//: &#x53D8;&#x91CF;&#x8BBE;&#x7F6E;&#x4E4B;&#x540E;&#xFF0C;&#x53EF;&#x4EE5;&#x7EE7;&#x7EED;&#x4FEE;&#x6539;&#x4E0D;&#x4F1A;&#x62A5;&#x9519;</span>
y = <span class="hljs-number">300</span>
</code></pre>
<h2 id="%E8%87%AA%E5%8A%A8%E6%8E%A8%E5%AF%BC">&#x81EA;&#x52A8;&#x63A8;&#x5BFC;</h2>
<ul>
<li>Swift&#x80FD;&#x591F;&#x6839;&#x636E;&#x53F3;&#x8FB9;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x63A8;&#x5BFC;&#x51FA;&#x53D8;&#x91CF;&#x7684;&#x51C6;&#x786E;&#x7C7B;&#x578B;</li>
<li>&#x901A;&#x5E38;&#x5728;&#x5F00;&#x53D1;&#x65F6;&#xFF0C;&#x4E0D;&#x9700;&#x8981;&#x6307;&#x5B9A;&#x53D8;&#x91CF;&#x7684;&#x7C7B;&#x578B;</li>
<li>&#x5982;&#x679C;&#x8981;&#x6307;&#x5B9A;&#x53D8;&#x91CF;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x53D8;&#x91CF;&#x540D;&#x540E;&#x4F7F;&#x7528;:&#xFF0C;&#x7136;&#x540E;&#x8DDF;&#x4E0A;&#x53D8;&#x91CF;&#x7684;&#x7C7B;&#x578B;</li>
</ul>
<blockquote>
<p>&#x91CD;&#x8981;&#x6280;&#x5DE7;&#xFF1A;Option + Click &#x53EF;&#x4EE5;&#x67E5;&#x770B;&#x53D8;&#x91CF;&#x7684;&#x7C7B;&#x578B;</p>
</blockquote>
<p><img src="images/option+click.png" alt=""></p>
<h2 id="%E9%9A%90%E5%BC%8F%E8%BD%AC%E6%8D%A2">&#x9690;&#x5F0F;&#x8F6C;&#x6362;</h2>
<ul>
<li>Swift &#x5BF9;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x8981;&#x6C42;&#x5F02;&#x5E38;&#x4E25;&#x683C;</li>
<li>&#x4EFB;&#x4F55;&#x65F6;&#x5019;&#xFF0C;&#x90FD;&#x4E0D;&#x4F1A;&#x505A;&#x9690;&#x5F0F;&#x8F6C;&#x6362;</li>
</ul>
<blockquote>
<p>&#x5982;&#x679C;&#x8981;&#x5BF9;&#x4E0D;&#x540C;&#x7C7B;&#x578B;&#x7684;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x8BA1;&#x7B97;&#xFF0C;&#x5FC5;&#x987B;&#x8981;&#x663E;&#x5F0F;&#x7684;&#x8F6C;&#x6362;</p>
</blockquote>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> x2 = <span class="hljs-number">10</span>
<span class="hljs-keyword">let</span> y2 = <span class="hljs-number">10.5</span>

<span class="hljs-keyword">let</span> r1 = <span class="hljs-type">Double</span>(x2) + y2
<span class="hljs-keyword">let</span> r2 = x2 + <span class="hljs-type">Int</span>(y2)
</code></pre>
<h2 id="let-var-%E7%9A%84%E9%80%89%E6%8B%A9">let &amp; var &#x7684;&#x9009;&#x62E9;</h2>
<ul>
<li><strong>&#x5E94;&#x8BE5;&#x5C3D;&#x91CF;&#x5148;&#x9009;&#x62E9;&#x5E38;&#x91CF;</strong>&#xFF0C;&#x53EA;&#x6709;&#x5728;&#x5FC5;&#x987B;&#x8981;&#x5BF9;&#x53D8;&#x91CF;&#x4FEE;&#x6539;&#x65F6;&#xFF0C;&#x624D;&#x9700;&#x8981;&#x4FEE;&#x6539;&#x4E3A; <code>let</code></li>
<li>&#x5728; Xcode 7.0 &#x4E2D;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x4FEE;&#x6539;&#x53D8;&#x91CF;&#xFF0C;Xcode &#x4F1A;&#x63D0;&#x793A;&#x4FEE;&#x6539;&#x4E3A; <code>let</code></li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./01-playground.html" class="navigation navigation-prev " aria-label="Previous page: Playground"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="./03-optional.html" class="navigation navigation-next " aria-label="Next page: Optional可选值"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
